//
//  RHDevEmsListVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/6.
//

#import "RHDevEmsListVC.h"
#import "RHDevEndPointDataModel.h"
#import "RHDeviceBatteryVC.h"

@interface RHDevEmsListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSArray *emsDataArr;

@property (nonatomic ,strong) UITableView *tableView;

@end

@implementation RHDevEmsListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"EMS";
    [self setupUI];
    [self getDevRelation];
}

- (void)setupUI{
    kWeakSelf;
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = CFTabViewBgColor;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self.view addSubview:_tableView];
    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@""];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 0, 0, 0));
    }];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
    
    [_tableView addHeaderRefresh:^{
        [weakSelf getDevRelation];
    }];
}


#pragma mark ------ network ---------

//获取端点关系
- (void)getDevRelation{
    [QMUITips showLoadingInView:self.view];
    [RHDevEndPointDataModel startGetWithCompletionAPIKey:H_getDevRelation argument:@{@"deviceId":self.deviceModel.deviceId} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            if(resultModel.code == 200){
                if(ValidDict(request.responseJSONObject[@"data"]))
                {
                    NSArray *arr = [NSArray yy_modelArrayWithClass:[RHDevEndPointDataModel class] json:request.responseJSONObject[@"data"][@"data"]];
                    //筛选出EMS
                    NSMutableArray *tempArr = [NSMutableArray array];
                    [arr enumerateObjectsUsingBlock:^(RHDevEndPointDataModel  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        if([obj.type isEqualToString:@"EMU"]){
                            [obj.nodes enumerateObjectsUsingBlock:^(RHDevEndPointDataModel  *_Nonnull obj2, NSUInteger idx, BOOL * _Nonnull stop) {
                                if([obj2.type isEqualToString:@"EMS"]){
                                    [tempArr addObject:obj2];
                                }
                            }];
                        }
                        if([obj.type isEqualToString:@"EMS"]){
                            [tempArr addObject:obj];
                        }
                    }];
                    self.emsDataArr = tempArr.copy;
                    [self.tableView reloadData];
                }
                
            }
            [self.tableView endHeaderRefresh];
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            [self.tableView endHeaderRefresh];
    }];
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.emsDataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    kWeakSelf;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"emsListCell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"emsListCell"];
    }
    RHDevEndPointDataModel *model = self.emsDataArr[indexPath.row];
    
    cell.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    [cell.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
   
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
    titleLab.font = kSYSTEMFONT(15);
    titleLab.text = model.type;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(-10);
    }];
    
    UIImageView *indicator = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:indicator];
    [indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.width.height.offset(15);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *subTitleLab = [[QMUILabel alloc]init];
    subTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    subTitleLab.font = kSYSTEMFONT(13);
    subTitleLab.text = model.sn;
    [bgView addSubview:subTitleLab];
    [subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.centerY.mas_offset(10);
    }];
    
  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    RHDevEndPointDataModel *emsModel = self.emsDataArr[indexPath.row];
    if(emsModel.nodes.count>0){
        RHDeviceBatteryVC *batVC = [[RHDeviceBatteryVC alloc]init];
        batVC.devModel = self.deviceModel;
        batVC.masterDataArr = emsModel.nodes;
        [self.navigationController pushViewController:batVC animated:YES];
    }else{
        [QMUITips showInfo:@"没有更多的数据".rh_localized];
    }
    

}
@end
